package com.algorithmicTest1;

public class BinarySearch {
    public static void main(String[] args) {
        //接下来实现二分查找：
        int []arr = {7,23,79,81,103,127,131,147};
        int Index = 131;
        int temp = binarySearch(arr,131);
        if(temp == -1){
            System.out.println("未找到");
        }else
            System.out.println("要找的索引是：" + temp);
    }
    public static int binarySearch(int[] arr, int Index) {
        //定义最小索引
        int minIndex = 0;
        //定义最大索引
        int maxIndex = arr.length - 1;
        int midIndex = 0;
        do{
            //定义中间索引
             midIndex = (maxIndex + minIndex)/2;
            //判断要查找的值大于中间索引的情况
            if(Index > arr[midIndex]){
                minIndex = midIndex + 1;
            }
            if(Index < arr[midIndex]){
                maxIndex = midIndex - 1;
            }
            if (arr[midIndex] == Index){
                return midIndex;
            }
        }while(minIndex <= maxIndex);

        return -1;

    }
}
